package vip.bblog.quartz.controller;

import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vip.bblog.common.utils.BaseResult;
import vip.bblog.quartz.entity.QuartzJob;
import vip.bblog.quartz.service.JobService;


/**
 * 调度中心
 *
 * @author An-Il
 */
@RestController
@RequestMapping(value = "/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @PostMapping
    public void saveOrUpdateJob(@RequestBody QuartzJob quartzJob) {
        jobService.saveOrUpdateJob(quartzJob);
    }

    @DeleteMapping
    public void delete(@RequestBody QuartzJob quartzJob) {
        jobService.deleteJob(quartzJob);
    }

    @PostMapping("/pause")
    public void pause(@RequestBody QuartzJob quartzJob) {
        jobService.pauseJob(quartzJob);
    }

    @PostMapping("/resume")
    public void resume(@RequestBody QuartzJob quartzJob) {
        jobService.resumeJob(quartzJob);
    }

    @GetMapping
    public BaseResult<Object> list(){
        return jobService.list();
    }

    @GetMapping("/checkCron")
    public boolean checkCron(@RequestParam String cron) {
        return CronExpression.isValidExpression(cron);
    }

}
